package com.example.tcp.reciver;


import com.alibaba.fastjson.JSONObject;
import com.example.common.ClientType;
import com.example.common.constant.Constants;
import com.example.common.enums.DeviceMultiLoginEnum;
import com.example.common.enums.command.SystemCommand;
import com.example.common.model.UserClientDto;
import com.example.proto.MessagePack;
import com.example.tcp.redis.RedisManager;
import com.example.tcp.utils.SessionSocketHolder;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.AttributeKey;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class UserLoginMessageListener {
    private static Logger logger = LoggerFactory.getLogger(UserLoginMessageListener.class);

    private Integer loginModel;

    public UserLoginMessageListener(Integer loginModel) {
        this.loginModel = loginModel;
    }

    public void listenerUserLogin(){
        RTopic topic = RedisManager.getRedissonClient().getTopic(Constants.RedisConstants.UserLoginChannel);   //进行获取到redis的路由  用来接收上线通知
        topic.addListener(String.class, new MessageListener<String>() {    //进行消息的监听
            @Override
            public void onMessage(CharSequence charSequence, String msg) {
                logger.info("收到用户上线");
                UserClientDto dto = JSONObject.parseObject(msg, UserClientDto.class);   // 将收到的消息进行转化
                List<NioSocketChannel> nioSocketChannels = SessionSocketHolder.get(dto.getAppId(), dto.getUserId());   //拿到所有在线的段
                for (NioSocketChannel nioSocketChannel : nioSocketChannels) {
                    //进行判断是那种登入模式 是单端双端还是什么
                    if(loginModel== DeviceMultiLoginEnum.ONE.getLoginMode()){    //单端
                        //获取原来的 clientType 和 imei
                        Integer clientType = (Integer) nioSocketChannel.attr(AttributeKey.valueOf(Constants.ClientType)).get();
                        String imei=(String) nioSocketChannel.attr(AttributeKey.valueOf(Constants.Imei)).get();
                        if(!(clientType+":"+imei).equals(dto.getClientType()+":"+dto.getImei())){   //
                            //执行踢出操作
                            MessagePack<Object> pack=new MessagePack<>();
                            pack.setToId((String)nioSocketChannel.attr(AttributeKey.valueOf(Constants.UserId)).get());
                            pack.setUserId((String)nioSocketChannel.attr(AttributeKey.valueOf(Constants.UserId)).get());
                            pack.setCommand(SystemCommand.MUTUALLOGIN.getCommand());
                            nioSocketChannel.writeAndFlush(pack);
                        }
                    }
                    else if(loginModel==DeviceMultiLoginEnum.TWO.getLoginMode()){   //二端
                        if(dto.getClientType() == ClientType.WEB.getCode()){    //这里进行判断是不是web段  是的话就跳过
                            continue;
                        }
                        Integer clientType = (Integer) nioSocketChannel.attr(AttributeKey.valueOf(Constants.ClientType)).get(); //检查当前端是不是 web端
                        if(clientType==ClientType.WEB.getCode()){
                            continue;
                        }
                        String imei=(String) nioSocketChannel.attr(AttributeKey.valueOf(Constants.Imei)).get();
                        if(!(clientType+":"+imei).equals(dto.getClientType()+":"+dto.getImei())){
                            //执行踢出操作
                            MessagePack<Object> pack=new MessagePack<>();
                            pack.setToId((String)nioSocketChannel.attr(AttributeKey.valueOf(Constants.UserId)).get());
                            pack.setUserId((String)nioSocketChannel.attr(AttributeKey.valueOf(Constants.UserId)).get());
                            pack.setCommand(SystemCommand.MUTUALLOGIN.getCommand());
                            nioSocketChannel.writeAndFlush(pack);
                        }
                    } else if (loginModel==DeviceMultiLoginEnum.THREE.getLoginMode()) {    //三端
                        Integer clientType = (Integer) nioSocketChannel.attr(AttributeKey.valueOf(Constants.ClientType)).get();
                        String imei=(String) nioSocketChannel.attr(AttributeKey.valueOf(Constants.Imei)).get();
                        if(dto.getClientType() == ClientType.WEB.getCode()){    // 登入的这里进行判断是不是web段  是的话就跳过
                            continue;
                        }

                        Boolean isSameClient = false;
                        if((clientType == ClientType.IOS.getCode() ||
                                clientType == ClientType.ANDROID.getCode()) &&
                                (dto.getClientType() == ClientType.IOS.getCode() ||
                                        dto.getClientType() == ClientType.ANDROID.getCode())){
                            isSameClient = true;
                        }

                        if((clientType == ClientType.MAC.getCode() ||
                                clientType == ClientType.WINDOWS.getCode()) &&
                                (dto.getClientType() == ClientType.MAC.getCode() ||
                                        dto.getClientType() == ClientType.WINDOWS.getCode())){
                            isSameClient = true;
                        }

                        if(isSameClient && !(clientType + ":" + imei).equals(dto.getClientType()+":"+dto.getImei())){
                            //执行踢出操作
                            MessagePack<Object> pack=new MessagePack<>();
                            pack.setToId((String)nioSocketChannel.attr(AttributeKey.valueOf(Constants.UserId)).get());
                            pack.setUserId((String)nioSocketChannel.attr(AttributeKey.valueOf(Constants.UserId)).get());
                            pack.setCommand(SystemCommand.MUTUALLOGIN.getCommand());
                            nioSocketChannel.writeAndFlush(pack);
                        }

                    }
                }



            }
        });
    }

}
